var add2 = document.getElementsByClassName('add2')[0];
var sure = document.getElementsByClassName('sure')[0];
var xiuGai = document.getElementsByClassName('xiuGai')[0];
var quxiao = document.getElementsByClassName('quxiao')[0];
var quxiao1 = document.getElementsByClassName('quxiao1')[0];
var bianJiId;
var newBianJiId;

// 弹窗是否出现
add2.addEventListener('click',function(){
    if(bianJiId){
        xiuGai.style.display = 'block';
        newBianJiId = bianJiId;
        bianJiId = false;
    }else {
        sure.style.display = 'block';
    }
 },false)
 quxiao.addEventListener('click',function(){
     sure.style.display = 'none';
  },false)
  quxiao1.addEventListener('click',function(){
    xiuGai.style.display = 'none';
 },false)
 function value(){
     //获取表单的值
    var user_nickname = $('#guanliname').val() ||null;
    var user_email = $('#email').val() ||null;
    var user_phone = $('#phone').val() ||null;
    var user_name = $('#username').val() ||null;
    var user_pass = $('#password1').val() ||null;
    var addInfo = {
              userNickname:user_nickname,
              userEmail:user_email,
              userPhone: user_phone,
              userName:user_name,
              userPass:user_pass,
            }
    return addInfo;
 }
//增加
$('.add').on('click',function(){
    var addInfo = value()
    // console.log('增加时的值',value())
    $.ajax({
        
        url:'/administrators/add',
        method:'GET',
        data:addInfo//获取表单的数据
    })
    .done(function(res){
        location.href = "../../pages/lzx_pages/lzx-list.html";
    })
    .fail(function(err){
        console.log('增加请求失败 msg22',err)
    })
});

//编辑的值
fuZhi()
function fuZhi(){
    var newData =  JSON.parse(sessionStorage.getItem('bianjiInfo'))
    console.log(newData)
    bianJiId = newData.user_id;
     $('#guanliname').val(newData.user_nickname);
    $('#email').val(newData.user_email);
    $('#phone').val(newData.user_phone);
    $('#username').val(newData.user_name);
    $('#password1').val(newData.user_pass);
    sessionStorage.setItem('bianjiInfo', '');
    console.log(bianJiId)
}
//点击编辑确定后修改编辑的值
$('.add3').on('click',function(){
    var bianJiInfo = value()
    $.ajax({
        url:'/administrators/xiuGai',
        method:'GET',
        data:{
            bianJiInfo:bianJiInfo,
            id:newBianJiId
        }
    })
    .done(function(res){
        console.log('编辑成功',res);
        location.href = "../../pages/lzx_pages/lzx-list.html";
    })
    .fail(function(err){
        console.log('编辑失败',err);
    })
    
   
})


// query(sql,null)
// .then (function(doc){
//   console.log(doc)
// })
// .catch(function(err){
//   console.log(err)
// })

// console.log('guanli',parent.administratorsInfo);
// //点击显示弹窗

// $('.add').on('click',function(){
//     var userid = $('#guanLiId').val();
//     var name = $('#guanliname').val();
//     var email = $('#email').val();
//     var phone = $('#phone').val();
//     var bumen = $('#bumen').val();
//     var quanXian = $('#quanXian').val();
//     var username = $('#username').val();
//     var password = $('#password1').val();
//     var addInfo = {
//       guanLiId: Number(userid),
//       name:name,
//       email:email,
//       phone: phone,
//       bumen:bumen,
//       check: false, 
//       username:username,
//       password:password,
//       settime:'2021-03-01',
//       lasttime:'2021-03-01',
//       loginPermission:true,
//       AdministrativePermissions:quanXian,
//     }
//       parent.administratorsInfo.push(addInfo)
//       //更新新增后的本地缓存数据
//       localStorage.setItem('administratorsInfo', JSON.stringify(parent.administratorsInfo))

//       sessionStorage.removeItem('userInfo');
// });
// //点击取消也删除编辑缓存
// $('.quxiao2').on('click',function(){
//   //清除编辑的缓存
//   sessionStorage.removeItem('userInfo');
// });

// //编辑获取值
// var userInfo = JSON.parse( sessionStorage.getItem('userInfo'));

// bianji();
// function bianji(){
//   var userid = $('#guanLiId').val(userInfo.guanLiId);
//   var name = $('#guanliname').val(userInfo.name);
//   var email = $('#email').val(userInfo.email);
//   var phone = $('#phone').val(userInfo.phone);
//   var bumen = $('#bumen').val(userInfo.bumen);
//   var quanXian = $('#quanXian').val(userInfo.AdministrativePermissions);
//   var username = $('#username').val(userInfo.username);
//   var password = $('#password1').val(userInfo.password);
// }
